/*
 * ******************************************************************************
 *   Copyright © 2013-2014 sean.yanghy. All rights reserved.
 *
 *  *****************************************************************************
 */

package com.exteam.common.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;

import com.exteam.common.util.CommLogger;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

/**
 * 父类Fragment
 *
 * @author sean.yanghy
 * @version v1.0
 * @created 2014-07-06 13:56:23
 */
public abstract class BaseFragment extends Fragment {

//	protected ImageLoader mImageLoader;

    public BaseController baseController;

    protected List<BaseController> baseControllers = new ArrayList<BaseController>(3);

    public BaseFragment() {
        // TODO Auto-generated constructor stub

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CommLogger.initLogger(this.getActivity().getClass().getSimpleName() + "-" + this.getClass().getSimpleName());
//		mImageLoader = ImageLoaderManager.getInstance();

    }

//	public ImageLoader getImageLoader() {
//		return mImageLoader;
//	}

    /**
     * 当这个分页被选中到当前显示时
     */
    public abstract void onSelected();

    /**
     * 刷新数据
     */
    public abstract void refresh();

    public void onRestart() {
        if (baseController != null) {
            baseController.onRestart();
        }
        for (BaseController baseCon : baseControllers) {
            baseCon.onRestart();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (baseController != null) {
            baseController.onStart();
        }
        for (BaseController baseCon : baseControllers) {
            baseCon.onStart();
        }
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        if (baseController != null) {
            baseController.onResume();
        }
        for (BaseController baseCon : baseControllers) {
            baseCon.onResume();
        }
    }

    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        if (baseController != null) {
            baseController.onPause();
        }
        for (BaseController baseCon : baseControllers) {
            baseCon.onPause();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (baseController != null) {
            baseController.onStop();
        }
        for (BaseController baseCon : baseControllers) {
            baseCon.onStop();
        }
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (baseController != null) {

            baseController.onDestroy();
            baseController.clear();
        }
        for (BaseController baseCon : baseControllers) {
            baseCon.onDestroy();
            baseCon.clear();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        CommLogger.d("fragment onActivityResult.....");
        super.onActivityResult(requestCode, resultCode, data);
        if (baseController != null) {
            baseController.onActivityResult(requestCode, resultCode, data);

        }
        for (BaseController baseCon : baseControllers) {
            baseCon.onActivityResult(requestCode, resultCode, data);
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

}
